<template>
  <div class="articletable-container">
    <el-table :data="tableData.filter((data) => !search || data.date.toLowerCase().includes(search.toLowerCase()))" style="width: 100%">
      <el-table-column label="发布时间" prop="created_time" width="180"> </el-table-column>
      <el-table-column label="作者" prop="author" width="120"> </el-table-column>
      <el-table-column label="标题" prop="title"> </el-table-column>
      <el-table-column align="right">
        <!-- eslint-disable-next-line -->
        <template slot="header" slot-scope="scope">
          <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
        </template>
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="toArticlePage(scope.row._id)">详情</el-button>
          <el-button size="mini" type="warning" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: 'ArticleTable',
  props: ['tableData'],
  data () {
    return {
      search: ''
    }
  },
  methods: {
    handleEdit (index, row) {
      console.log(index, row)
    },
    async handleDelete (index, row) {
      // console.log(index, row)
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showCancelButton: true,
        type: 'warning'
      })
        .then(async () => {
          const res = await this.$api.deleteArticle({
            id: row._id
          })
          if (res.data.err_code === 0) {
            this.$notify({
              title: '成功',
              type: 'success',
              message: '删除成功!'
            })
            // 删除成功后重新刷新数据
            this.initArticlesInfo()
          }
        })
        .catch(() => {
          this.$notify.info({
            title: '成功',
            message: '已取消删除'
          })
        })
    },
    toArticlePage (val) {
      // console.log(val)
      this.$router.push('/blog/article/' + val)
    }
  }
}
</script>

<style lang="less" scoped></style>
